package com.ailanyin.modules.system.controller;

import com.ailanyin.common.base.domain.BaseQuery;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.common.log.annotation.Log;
import com.ailanyin.common.log.enums.BusinessType;
import com.ailanyin.common.poi.utils.ExcelUtil;
import com.ailanyin.modules.system.domain.SysLoginLog;
import com.ailanyin.modules.system.service.ISysLoginLogService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登录日志
 *
 * @author ailanyin
 */
@RestController
@RequestMapping("/monitor/loginLog")
public class SysLoginLogController {

    @Autowired
    private ISysLoginLogService loginLogService;


    @PreAuthorize("hasAuthority('monitor:loginLog:list')")
    @GetMapping("/list")
    public CommonPage list(SysLoginLog loginLog,
                           @RequestParam(value = "pageSize" , defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysLoginLog> list = loginLogService.selectLoginLogList(loginLog);
        return CommonPage.restPage(list);
    }

    @Log(title = "登录日志" , businessType = BusinessType.EXPORT)
    @PreAuthorize("hasAuthority('monitor:loginLog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLog loginLog) throws Exception {
        List<SysLoginLog> list = loginLogService.selectLoginLogList(loginLog);
        ExcelUtil.exportExcel(list, SysLoginLog.class, "登录日志" , response);
    }

    @PreAuthorize("hasAuthority('monitor:loginLog:remove')")
    @Log(title = "登录日志" , businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody BaseQuery<Long> query) {
        return AjaxResult.toAjax(loginLogService.deleteLoginLogByIds(query.getIds()));
    }

    @PreAuthorize("hasAuthority('monitor:loginLog:remove')")
    @Log(title = "登录日志" , businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        loginLogService.cleanLoginLog();
        return AjaxResult.success();
    }

}
